/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       udpconnection.h
 * Author:     karooolek
 * Created on: 2009-03-14
 *
 **********************************************************************************************************************/

#ifndef UDPCONNECTION_H_
#define UDPCONNECTION_H_

#include "ipconnection.h"

namespace mGameEngine
{
namespace Network
{


/**
 * UDP/IP connection.
 * Network connection with remote machine over UDP/IP datagram protocol.
 * UDP connection is connection-less - it doesn't connect exactly two
 * machines. Datagrams can be sent to UDP server from various machines
 * and server can broadcast data to many clients.
 * Using UDP/IP connection doesn't guarantee 100% correct transmission
 * or information about connection state.
 */
class UDPConnection: public IPConnection
{
public:
    /**
     * Create new UDP/IP connection
     */
    UDPConnection();

    /**
     * Create new UDP/IP connection with specified address.
     * @param ipaddr IP address.
     */
    UDPConnection(const IPAddress &ipaddr);

    /**
     * Destroy UDP/IP connection.
     */
    ~UDPConnection();

    /**
     * Open UDP/IP connection.
     */
    void open();

    /**
     * Close UDP/IP connection.
     */
    void close();

    /**
     * Connect to UDP/IP connection server.
     */
    void connect();

    /**
     * Disconnect from UDP/IP connection server.
     */
    void disconnect();

    /**
     * Send data to another machine using UDP/IP protocol
     * @param data data to be sent.
     * @param size buffer size.
     * @return number of sent bytes os 0 if connection was lost.
     */
    virtual uint send(const uint8 *data, uint size);

    /**
     * Receive data from another machine.
     * @param buffer buffer to store received data.
     * @param size buffer size.
     * @return number of received bytes or 0 if connection was lost.
     */
    virtual uint receive(uint8 *buffer, uint size);
};

}
}

#endif // UDPCONNECTION_H_
